package tests.db;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.ArrayList;

import struct.*;
import db.DBManager;
import db.DBQuery;
import java.sql.ResultSet;

/**
 * Tests Interest(tables include: fInterest, aInterest, dInterest) and
 *  History (tables include: fHistory, aHistory, dHistory) related methods.
 * @author Gabrielle DuBois
 */
public class DBManagerTestQueries {
    private DBManager dbm;
    private DBQuery dbq;

   @Before
    public void setUp() throws Exception{
        //todo before each @Test
        dbm = new DBManager();
        dbq = new DBQuery();
    }

   @After
    public void tearDown() throws Exception {
        //todo after each @Test
        dbm.close();
        dbq.close();
    }

   @Test
    public void testGeneralQueriesAdults() throws Exception{
        String table = "adults";
        String keyword = "Self";
        ResultSet rs = dbq.generalQuery(table, keyword);
        boolean success = false;

        int numColumns = rs.getMetaData().getColumnCount();
        String rowString;

        System.out.println("Contents of Search on: " + table + " AND "+ keyword);
        while (rs.next()){
            success = true;
            rowString = "";
            for (int i=1; i<numColumns; i++){
                rowString += rs.getString(i) + ", ";
            }
            System.out.println(rowString.substring(0,rowString.length()-2));
        }
        System.out.println("");

        assertTrue(success);
    }//testGeneralQueriesAdults()

   @Test
    public void testGeneralQueriesDependents() throws Exception{
        String table = "dependents";
        String keyword = "20";
        ResultSet rs = dbq.generalQuery(table, keyword);
        boolean success = false;

        int numColumns = rs.getMetaData().getColumnCount();
        String rowString;

        System.out.println("Contents of Search on: " + table + " AND "+ keyword);
        while (rs.next()){
            success = true;
            rowString = "";
            for (int i=1; i<numColumns; i++){
                rowString += rs.getString(i) + ", ";
            }
            System.out.println(rowString.substring(0,rowString.length()-2));
        }
        System.out.println("");
        
        assertTrue(success);
    }//testGeneralQueriesDependents()

   @Test
    public void testGeneralQueriesFamilies() throws Exception{
        String table = "families";
        String keyword = "Benloit";
        ResultSet rs = dbq.generalQuery(table, keyword);
        boolean success = false;

        int numColumns = rs.getMetaData().getColumnCount();
        String rowString;

        System.out.println("Contents of Search on: " + table + " AND "+ keyword);
        while (rs.next()){
            success = true;
            rowString = "";
            for (int i=1; i<numColumns; i++){
                rowString += rs.getString(i) + ", ";
            }
            System.out.println(rowString.substring(0,rowString.length()-2));
        }
        System.out.println("");

        assertTrue(success);
    }//testGeneralQueriesFamilies()

   @Test
    public void testGeneralQueriesMinistries() throws Exception{
        String table = "ministries";
        String keyword = "er";
        ResultSet rs = dbq.generalQuery(table, keyword);
        boolean success = false;

        int numColumns = rs.getMetaData().getColumnCount();
        String rowString;

        System.out.println("Contents of Search on: " + table + " AND "+ keyword);
        while (rs.next()){
            success = true;
            rowString = "";
            for (int i=1; i<numColumns; i++){
                rowString += rs.getString(i) + ", ";
            }
            System.out.println(rowString.substring(0,rowString.length()-2));
        }
        System.out.println("");

        assertTrue(success);
    }//testGeneralQueriesMinistries()

   @Test
    public void testGeneralQueriesAddress() throws Exception{
        String table = "address";
        String keyword = "Neenah";
        ResultSet rs = dbq.generalQuery(table, keyword);
        boolean success = false;

        int numColumns = rs.getMetaData().getColumnCount();
        String rowString;

        System.out.println("Contents of Search on: " + table + " AND "+ keyword);
        while (rs.next()){
            success = true;
            rowString = "";
            for (int i=1; i<numColumns; i++){
                rowString += rs.getString(i) + ", ";
            }
            System.out.println(rowString.substring(0,rowString.length()-2));
        }
        System.out.println("");

        assertTrue(success);
    }//testGeneralQueriesAddress()

   @Test
    public void testGeneralQueriesMoney() throws Exception{
        String table = "money";
        String keyword = "2009";
        ResultSet rs = dbq.generalQuery(table, keyword);
        boolean success = false;

        int numColumns = rs.getMetaData().getColumnCount();
        String rowString;

        System.out.println("Contents of Search on: " + table + " AND "+ keyword);
        while (rs.next()){
            success = true;
            rowString = "";
            for (int i=1; i<=numColumns; i++){
                rowString += rs.getString(i) + ", ";
            }
            System.out.println(rowString.substring(0,rowString.length()-2));
        }
        System.out.println("");

        assertTrue(success);
    }//testGeneralQueriesMoney()

   @Test
    public void testGetAllFamilyKeys() throws Exception{
        ArrayList<String> fk = dbm.getAllFamilyKeys();
        String[] temp;

        System.out.println("All Family Keys: ");
        for (int i=0; i<fk.size(); i++){
            System.out.println(fk.get(i));
        }

        System.out.println("");

        assertTrue(fk.size() > 0);

        //For the lovely Sarah:
        //http://download.oracle.com/javase/6/docs/api/java/lang/String.html
        System.out.println("Spliting up the combined keys: ");
        for (int i=0; i<fk.size();i++){
            temp = fk.get(i).split(";");
            System.out.print("( ");
            for (int j=0;j<temp.length;j++){
                System.out.print("'"+temp[j]+"' ");
            }
            System.out.println(")");
        }
        System.out.println("");

    }//testGetAllFamilyKeys()

   @Test
    public void getAllIndividualKeysAdults() throws Exception{
        ArrayList<String> ak = dbm.getAllIndividualKeys("adults");
        String[] temp;

        System.out.println("All Adult Keys: ");
        for (int i=0; i<ak.size(); i++){
            System.out.println(ak.get(i));
        }

        System.out.println("");

        assertTrue(ak.size() > 0);

        //For the lovely Sarah:
        //http://download.oracle.com/javase/6/docs/api/java/lang/String.html
        System.out.println("Spliting up the combined keys: ");
        for (int i=0; i<ak.size();i++){
            temp = ak.get(i).split(";");
            System.out.print("( ");
            for (int j=0;j<temp.length;j++){
                System.out.print("'"+temp[j]+"' ");
            }
            System.out.println(")");
        }
        System.out.println("");

    }//getAllIndividualKeysAdults()

   @Test
    public void getAllIndividualKeysDependents() throws Exception{
        ArrayList<String> ak = dbm.getAllIndividualKeys("dependents");
        String[] temp;

        System.out.println("All Dependent Keys: ");
        for (int i=0; i<ak.size(); i++){
            System.out.println(ak.get(i));
        }

        System.out.println("");

        assertTrue(ak.size() > 0);
    }//getAllIndividualKeysDependents()

   @Test
    public void testGetIndividualVolunteeringAdult() throws Exception{
       /*
        boolean success = false;

       String first = "Joe";
       String last = "Barnier";
       String dob = "12/31/1985";
       String[] k = new String[3];
       k[0] = first;
       k[1] = last;
       k[2] = dob;

       Volunteer v = dbm.getVolunteering("adults", k);

       ArrayList<String[]> i = v.getInterest();
       ArrayList<String[]> h = v.getHistory();
       String[] temp;

       if (i.size()>0){
           success = true;

           System.out.println("Volunteer Interest of: "+first+" "+last+" "+dob);
           for(int m=0;m<i.size();m++){
               temp = i.get(m);
               for(int n=0;n<temp.length;n++){
                    System.out.print("'"+temp[n]+"' ");
               }
               System.out.println("");
           }
           System.out.println("");
       }
       assertTrue(success);
       success = false;

       if(h.size()>0){
           success = true;
           System.out.println("Volunteer History of: "+first+" "+last+" "+dob);
           for(int m=0;m<h.size();m++){
               temp = h.get(m);
               for(int n=0;n<temp.length;n++){
                    System.out.print("'"+temp[n]+"' ");
               }
               System.out.println("");
           }
           System.out.println("");
       }
       assertTrue(success);
        *
        */
   }

   @Test
    public void testGetIndividualVolunteeringFamily() throws Exception{
       /*
       boolean success = false;

       String name = "DuBois";
       String phone = "(920)555-5555";
       String[] k = new String[2];
       k[0] = name;
       k[1] = phone;

       Volunteer v = dbm.getVolunteering("families", k);

       ArrayList<String[]> i = v.getInterest();
       ArrayList<String[]> h = v.getHistory();
       String[] temp;

       if (i.size()>0){
           success = true;

           System.out.println("Volunteer Interest of: "+name+" "+phone);
           for(int m=0;m<i.size();m++){
               temp = i.get(m);
               for(int n=0;n<temp.length;n++){
                    System.out.print("'"+temp[n]+"' ");
               }
               System.out.println("");
           }
           System.out.println("");
       }
       assertTrue(success);
       success = false;

       if(h.size()>0){
           success = true;
           System.out.println("Volunteer History of: "+name+" "+phone);
           for(int m=0;m<h.size();m++){
               temp = h.get(m);
               for(int n=0;n<temp.length;n++){
                    System.out.print("'"+temp[n]+"' ");
               }
               System.out.println("");
           }
           System.out.println("");
       }
       assertTrue(success);
        * 
        */
   }

}//DBManagerTestQueries